<template>
	<el-dialog v-model="props.sureVisible" title="参会确认" width="60%" :before-close="() => close(false)">
		<div v-loading="loading">
			<HyForm :hyFormDefault="hyFormDefault" :hyFormConfig="hyFormConfig" :hyFormFields="hyFormFields" ref="hyFormRef" />
		</div>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="close(false)">{{ sureType === 'sure' ? '取消' : '关闭' }}</el-button>
				<el-button v-if="sureType === 'sure'" type="primary" @click="subumit" :loading="loading"> 确定 </el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import HyForm from '/@/components/HyForm/index.vue';
import { meetingSureApi, meetingCountApi } from '/@/api/expertDatabase/extract';
import { useDict } from '/@/hooks/dict';

const props = defineProps({
	sureVisible: Boolean,
	id: {
		type: Array,
		default: () => [],
	},
	sureType: {
		type: String,
		default: '',
	},
	refuseReason: {
		type: String,
		default: '',
	},
});

const hyFormDefault = ref({});
const loading = ref(false);
watch(
	() => props.sureVisible,
	() => {
		if (props.sureVisible && props.id) {
			loading.value = true;
			meetingCountApi({ id: props.id })
				.then((res) => {
					nextTick(() => {
						if (props.sureType === 'sure') {
							hyFormDefault.value = {
								notHasReasonCount: res.notHasReasonCount,
								turnDownCount: res.turnDownCount,
								refuseReason: '',
							};
							change('1');
						}
						if (props.sureType === 'view') {
							change('0');
							hyFormDefault.value = {
								...hyFormDefault.value,
								refuseReason: props.refuseReason,
							};
						}
					});
				})
				.finally(() => {
					loading.value = false;
				});
		}
	}
);
// 定义字典
const { yes_no_type } = useDict('yes_no_type');

//动态表单配置
const hyFormConfig = computed(() => {
	return {
		labelWidth: '120px',
		type: props.sureType === 'view' ? 'view' : 'submit',
	};
});
//配置基本信息表单
const hyFormFields = computed(() => {
	return {
		turnDownCount: {
			label: '已拒绝参会',
			type: 'input',
			disabled: true,
		},
		notHasReasonCount: {
			label: '无故缺席',
			type: 'input',
			disabled: true,
		},
		attend: {
			label: '是否参会',
			type: 'select',
			options: yes_no_type.value,
			required: true,
			remove: props.sureType === 'view',
			change: (value: string) => change(value),
		},
		refuseReason: {
			label: '拒绝参会理由',
			type: 'textarea',
			required: true,
			remove: hiddenReson.value,
		},
	};
});
//改变
const hiddenReson = ref(true);
const change = (value: string) => {
	if (value === '0') {
		hiddenReson.value = false;
	} else {
		hiddenReson.value = true;
	}
};

//确定
const hyFormRef = ref();
const subumit = async () => {
	const result = await hyFormRef.value?.check();
	const params = {
		...hyFormRef.value.hyForm,
		id: props.id,
	};
	if (result) {
		loading.value = true;
		meetingSureApi(params)
			.then(() => {
				close(true);
			})
			.finally(() => {
				loading.value = false;
			});
	}
};
//关闭
const emits = defineEmits(['close']);
const close = (isRefresh: boolean) => {
	emits('close', isRefresh);
};
</script>

<style scoped lang="scss"></style>
